package com.asia5b.a5mall.middleware.logic.viewmodel.goods

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.goods.GoodsSelectorModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.removeAllOffGoods
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/1/11.
 */
class SearchGoodsActivityVM : SimpleViewModel() {

   private var page = 1
   private var sumPage = 1000


    val goodsListLiveData = MutableLiveData<ResultModel<ArrayList<AbsGoodsModel>>>()
    fun getDatas(refresh: Int, params: HashMap<String, Any>) {
        if (refresh <= 1) {
            page = 1
            sumPage = 1000
        }
        if (page > sumPage) {
            dismissLoadingBar()
            return
        }

        val observable = goodsApi.searchGoodsList(page, 10, params)
        baseSubscribe(observable, Consumer {
            sumPage = it.sumPage
            page = it.page + 1//预备加载下一页
            if (it.data != null) {
                it.data = removeAllOffGoods(it.data!!)
            }
            goodsListLiveData.value = it
        })

    }

    val searchGoodsSelectorLiveData = MutableLiveData<GoodsSelectorModel>()
    fun searchGoodsSelector(params: HashMap<String, Any>) {
        baseSubscribe(goodsApi.searchGoodsSelector(page, 10, params), Consumer {
//            providerView().searchGoodsSelectorResult(it)
            searchGoodsSelectorLiveData.value = it
        })
    }
}